import { ListBase } from 'src/components/list_base_abs';
import { Component, Emit, Prop, Provide, Watch } from 'vue-property-decorator'

import { auditManageConst } from "src/consts/audit_manage_const";
import { updateSelection } from 'src/utils/list_utils';
import OperatorHandler from 'src/views/audit_manage/operator_handler';
import ListDemoHandler from 'src/views/audit_manage/list_handler';
import * as Api from 'src/api/net';

@Component({})
export default class AuditManage extends ListBase {
    operatorData: OperatorHandler;
    listHandler: ListDemoHandler;
    isAudit: Boolean;
    // isAdmin: Boolean;
    // reqUrl: String;
    activeRouteItemRoute: string;
    mainData: any;
    advData: any;
    queryData: any;
    feature_code: any;
    expandedList: any

    initData(that: AuditManage) {
        that.myState = this.$store.state.auditManage;
        that.selection = [];
        that.operatorData = new OperatorHandler();
        that.listHandler = new ListDemoHandler();
        that.operatorData.init(this);
        that.isAudit = false
        // var user = getUserInfo()
        // that.isAdmin = false
        // for (let i = 0; i < user.roles.length; i++) {
        //     if (user.roles[i].id == 1) {
        //         that.isAdmin = true
        //     }
        // }

        // if (that.isAdmin) {
        //     that.reqUrl = "/website/audit/admin/list.do"
        // } else {
        //     that.reqUrl = "/website/audit/normal/list.do"
        // }

        that.mainData = {}
        that.advData = {}
        that.queryData = {};
        that.feature_code = Config.featureCode2 + 'web.model.WebsiteView'
        that.expandedList = {}
    }

    getListData(): void {
        let sendData: any = {}
        sendData["curr_page"] = this.myState.curr_page
        sendData["page_size"] = this.myState.page_size
        WLDispatch(auditManageConst.DispatchAuditMList, sendData);
    }

    mounted(): void {
        this.getListData();
    }

    expandChange(row: any, expandedRows: any): void {

    }

    rowClick(row: any, event: any, column: any) {
        if (column && !column.label) {
            if (row.ind) {
                this.$refs.auditMTable.toggleRowExpansion(row, false)
                row.ind = false;
            } else {
                row.audit_status = getValueByKey("audit_status", row.status)
                var sendData: any = {}
                sendData.entity_id = row.entity_id
                sendData.model = row.model
                Api.AjaxPost(Config.ajaxUrl + "/webaudit/audit/view.do", sendData, (res: any) => {
                    if (isNoEmptyObject(res.data)) {
                        row.create_time = timestampToTime(res.data.create_time)
                        for (let i = 0; i < res.data.nodes.length; i++) {
                            if (res.data.nodes[i].status == "checking") {
                                if (res.data.nodes[i].value_type != "leader") {
                                    row.nowRole = res.data.nodes[i].value_name
                                } else {
                                    row.nowRole = "上级领导"
                                }
                                if (i != 1) {
                                    if (res.data.nodes[i - 1].value_type != "leader") {
                                        row.lastRole = res.data.nodes[i - 1].value_name
                                    } else {
                                        row.lastRole = "上级领导"
                                    }
                                    row.auditDate = timestampToTime(res.data.nodes[i - 1].end_time)
                                }
                            }


                            if (row.status == "pass" || row.status == "refuse") {
                                if (res.data.nodes[res.data.nodes.length - 1].value_type != "leader") {
                                    row.lastRole = res.data.nodes[res.data.nodes.length - 1].value_name
                                } else {
                                    row.lastRole = "上级领导"
                                }
                                row.auditDate = timestampToTime(res.data.nodes[res.data.nodes.length - 1].end_time)
                            }
                        }
                        this.$refs.auditMTable.toggleRowExpansion(row, true)
                        row.ind = true;

                    }
                })
            }
        }
    }

    doQuery(isAdv: Boolean): void {
        var obj: any = {}
        for (let key in this.mainData) {
            if (this.mainData[key]) {
                obj[key] = this.mainData[key]
                if (key == "apply_account") {
                    obj[key] = this.mainData[key].name
                }
            }
        }

        if (isAdv) {
            for (let key in this.advData) {
                if (this.advData[key]) {
                    obj[key] = this.advData[key]
                    if (key == "date") {
                        obj.start_date = this.advData[key][0]
                        obj.end_date = this.advData[key][1]
                    }
                }
            }
        }


        this.queryData = obj

        obj["curr_page"] = this.myState.curr_page
        obj["page_size"] = this.myState.page_size
        WLDispatch(auditManageConst.DispatchAuditMList, obj);
    }

    get _activeRouteItemRoute(): string {
        return this.$route.path
    }

    @Watch('_activeRouteItemRoute', { immediate: true })
    onRouteChanged(val: any) {
        if (val == "/audit_manage") {
            this.getListData()
        }
    }

    sizeChange(size: number): void {
        let senData: any = this.queryData
        senData["curr_page"] = this.myState.curr_page
        senData["page_size"] = size
        WLDispatch(auditManageConst.DispatchAuditMList, senData);
    }

    pageChange(page: number): void {
        let senData: any = this.queryData
        senData["curr_page"] = page
        senData["page_size"] = this.myState.page_size
        WLDispatch(auditManageConst.DispatchAuditMList, senData);
    }

    handleSelectionChange(list: any[]): void {
        updateSelection(this.selection, list);
        this.handleBtnShow(this.selection)
        console.log("this selection", this.selection, this.selection.length);
    }

    handleBtnShow(data: any): void {
        if (data.length > 0) {
            this.isAudit = true
            for (let i = 0; i < data.length; i++) {
                if (data[i].status != "checking") {
                    this.isAudit = false
                }

                var flag = false
                if (data[i].curNode && isNoEmpty(data[i].curNode.user_list)) {
                    for (let k = 0; k < data[i].curNode.user_list.length; k++) {
                        // if (isNoEmpty(getUserInfo().roles)) {
                        //     for (let j = 0; j < getUserInfo().roles.length; j++) {
                        //         if (getUserInfo().roles[j].id == data[i].curNode.user_list[k].id) {
                        //             flag = true
                        //         }
                        //     }
                        // }

                        if (getUserInfo().userId == data[i].curNode.user_list[k].id) {
                            flag = true
                        }
                    }
                }
                if (!flag) {
                    this.isAudit = false
                    return
                }
            }
        } else {
            this.isAudit = false
        }
    }

    rowClassName(data: any): string {
        if (data.row.selected) {
            return 'selected_row';
        }
        return '';
    }

    doAudit(): void {
        if (this.operatorData && this.operatorData.doAudit) {
            this.operatorData.doAudit(this.selection);
        } else {
            this.$message.warning("todo doAudit 方法");
        }
    }

    rowEdit(ind: number, row: DemoData): void {
        if (this.listHandler && this.listHandler.rowEdit) {
            this.listHandler.rowEdit(ind, row);
        } else {
            console.warn("todo 初始化和实现行内容详情");
        }
    }

    rowSet(ind: number, row: DemoData): void {
        if (this.listHandler && this.listHandler.rowSet) {
            this.listHandler.rowSet(ind, row);
        } else {
            console.warn("todo 初始化和实现行内容审核详情");
        }
    }

}